package it.danghongphat.list;
import it.danghongphat.business.MaterialBusiness;
import it.danghongphat.gui.MainForm;
import it.danghongphat.gui.windowms.MaterialGui;
import it.danghongphat.object.Material;

import java.awt.BorderLayout;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
//import java.io.FileInputStream;
//import java.io.InputStream;
//import java.util.ArrayList;
//import java.util.HashMap;
import java.util.List;
//import java.util.Map;

import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.table.AbstractTableModel;

//import net.sf.jasperreports.engine.JasperCompileManager;
//import net.sf.jasperreports.engine.JasperExportManager;
//import net.sf.jasperreports.engine.JasperFillManager;
//import net.sf.jasperreports.engine.JasperPrint;
//import net.sf.jasperreports.engine.JasperReport;
//import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
//import net.sf.jasperreports.engine.design.JasperDesign;
//import net.sf.jasperreports.engine.xml.JRXmlLoader;

public class ListMaterial extends JPanel implements MouseListener{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	public MyTableModel myTableModel;
	public JTable table ;
	String action=null;
	MainForm main;
	public List<Material> list;
	public String getAction(){
		return action;
	}
	
	public void setAction(String action){
		 this.action = action;
	}
	public ListMaterial(MainForm main) {
		super(new BorderLayout());
		this.main=main;
		myTableModel=new MyTableModel();
		table= new JTable(myTableModel);
		table.addMouseListener(this);
		table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		JScrollPane scrollpane = new JScrollPane(table);
		add(scrollpane, BorderLayout.CENTER);
		
	}

	@Override
	public void mouseClicked(MouseEvent e) {
		if(e.getClickCount()==2 && !e.isConsumed()){
			int[] index = table.getSelectedRows();
			MaterialGui materialGui=new MaterialGui(main);
			materialGui.buttonAction.setText("Update");
			materialGui.setVisible(true);
			Material m=(Material)list.get(index[0]);
			materialGui.setTextMaterial(m);
		}
	}
	@Override
	public void mouseEntered(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void mouseExited(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void mousePressed(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void mouseReleased(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}
public class MyTableModel extends AbstractTableModel {
		private static final long serialVersionUID = 1L;
		MaterialBusiness materialBusiness;
		public MyTableModel(){
			materialBusiness=new MaterialBusiness(this);
			list=materialBusiness.LoadListMaterialList();
		}
		@Override
		public String getColumnName(int i) {
		    return materialBusiness.getHeader()[i];
		}
		@Override
		public int getColumnCount() {
			return 5;
		}
	
		@Override
		public int getRowCount() {
			return list.size();
		}
		@Override
		public Object getValueAt(int row, int col) {
			Material m=(Material)list.get(row);
			switch(col) {
            case 0: return m.getMah();
            case 1: return m.getTenh();
            case 2: return m.getManh();
            case 3: return m.getDvt();
            case 4: return m.getDb();
			}
			return null;
		}
	
	}
}
